package com.sxpi.upload.controller.upload;

import com.sxpi.upload.controller.BaseController;
import com.sxpi.upload.service.CosUploadService;
import com.sxpi.common.exception.PugBusinessException;
import com.sxpi.common.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.HashMap;

/**
 * @Author: 董伟豪
 * @Date: 2022/10/19 10:24
 */
@RestController
@Slf4j
public class CosUploadController extends BaseController {

    @Autowired
    private CosUploadService cosUploadService;

    /**
     *
     * @param file 上传的文件
     * @param module 执行文件上传的云目录
     * @return
     */
    @PostMapping("/cos/file")
    // @SaCheckLogin 由于自身的限制无法使用登录验证
    public R uploadCosPrivateFile(@RequestParam("file") MultipartFile file,
                                  @RequestParam(name = "module",required = false,defaultValue = "xxb") String module){
        if(file.isEmpty()){
            throw new PugBusinessException("上传文件不能为空");
        }
        try{
            // 文件上传
            HashMap map = cosUploadService.uploadPublicFile(file,module);
            // 重要，紧急的
            // 不重要，紧急的
            // 重要，不紧急的
            // 上传返回
            return R.ok(map);
        }catch (Exception e){
            log.error("文件上传到腾讯云错误", e);
            throw new PugBusinessException("文件上传到腾讯云错误");
        }
    }
}
